<?php
/**
 * 真题管理
 * Created by PhpStorm.
 * User: Administrator
 * Date: 15-6-17
 * Time: 下午2:37
 */
namespace app\modules\weixin\controllers;

use app\libs\Method;
use app\modules\test\models\TestCate;
use app\modules\test\models\TestPublic;
use app\modules\weixin\models\LowProduct;
use app\modules\weixin\models\LowProductClass;
use yii;
use app\libs\ApiControl;

class ProductController extends ApiControl
{
    public $layout = 'test';
    public $enableCsrfValidation = false;

    public function actionIndex()
    {
        $page = Yii::$app->request->get('page', 1);
        $data = LowProduct::find()->asArray()->all();
        return $this->render('index', ['data' => $data, 'page' => $page]);
    }

    public function actionAdd(){
        $post = Yii::$app->request->post();
        if($post){
            if($post['id']){
                LowProduct::updateAll($post,"id={$post['id']}");
            }else{
                Yii::$app->db->createCommand()->insert("{{%low_product}}",$post)->execute();
            }
            return $this->redirect("/weixin/product/index");
        }
        return $this->render('add');
    }

    public function actionUpdate(){
        $id = Yii::$app->request->get('id');
        $data = LowProduct::find()->where("id=$id")->one();
        return $this->render('add',['data' => $data]);
    }

    public function actionClass()
    {
        $page = Yii::$app->request->get('page', 1);
        $id = Yii::$app->request->get('id');
        $count = LowProductClass::find()->select('id')->count();
        $offset = ($page-1)*10;
        $data = LowProductClass::find()->where("productId=$id")->offset($offset)->limit(10)->all();
        $page = Method::getPagedRows(['count' => $count, 'pageSize' => 10, 'rows' => 'models']);
        return $this->render('class', ['data' => $data, 'page' => $page]);
    }

    public function actionImportClass(){
        $id = Yii::$app->request->post('id');
        $file = Yii::$app->request->post('file');
        $data  = Method::importExcel($file);
        unset($data['data'][1]);
        foreach ($data['data'] as $v){
            $time = str_replace('：',':',$v['D']);
//            $time = explode("-",$time);
            $date = str_replace('x','-',$v['B']);
            $arr = [
                'name' => $v['J'],
                'date' => $date,
                'startTime' => $time,
                'endTime' => '',
                'section' => $v['F'],
                'productId' => $id,
                'createTime' => time()
            ];
            Yii::$app->db->createCommand()->insert("{{%low_product_class}}",$arr)->execute();
        }
        die(Method::jsonGenerate(1));
    }

    /**
     * 添加课程
     */
    public function actionAddClass(){
        $post = Yii::$app->request->post();
        Yii::$app->db->createCommand()->insert("{{%low_product_class}}",$post)->execute();
        die(Method::jsonGenerate(1));
    }

    /**
     * 修改课程
     */
    public function actionUpdateClass(){
        $post = Yii::$app->request->post();
        LowProductClass::updateAll($post,"id={$post['id']}");
        die(Method::jsonGenerate(1));
    }

    public function actionDeleteClass(){
        $id = Yii::$app->request->get('id');
        $class = LowProductClass::findOne($id);
        $productId = $class->productId;
        $class->delete();
        return $this->redirect("/weixin/product/class?id=".$productId);
    }


}